package alarmapi

import (
	ca "iot-base/common/cache/alarm"
	"iot-base/common/dbconn"
	"iot-base/common/eventtype"
	"iot-base/common/notify"
	"iot-base/common/webapi"
	"strconv"

	"github.com/gin-gonic/gin"
)

func InitAlarmAPI(api *gin.RouterGroup) {
	alarmAPI := api.Group("alarm")
	{
		alarmAPI.POST("/edge/:id", func(c *gin.Context) {
			id := c.Param("id")
			if id == "" {
				webapi.SendError("id cannot empty", webapi.ParamsError, c)
				return
			}
			notify.PubMsg(true, "syncEgdeAlarm", id)
			webapi.SendOK(c)
		})
		alarmAPI.PUT("/sync", func(c *gin.Context) {
			ca.SyncAlarms()
			webapi.SendOK(c)
		})
		alarmAPI.PUT("/force", func(c *gin.Context) {
			var alarm eventtype.Alarm
			err := c.ShouldBind(&alarm)
			if err != nil || alarm.TID == "" || alarm.AlarmID == "" {
				webapi.SendError("参数错误", webapi.ParamsError, c)
				return
			}
			alarm.Checker = "client_force"
			notify.RawAlarm(&alarm)

			webapi.SendOK(c)

		})
		alarmAPI.GET("/real", func(c *gin.Context) {
			tid := c.Query("tid")
			aid := c.Query("aid")
			sid := c.Query("stationId")
			//sid := c.Query("subid")
			sindex := c.Query("index")

			sql := "select * from alarm_states where 1=1 and is_alarm=true "
			if sindex != "" {
				sql = sql + "and mindex=" + sindex
			}
			if tid != "" {
				sql = sql + " and t_id='" + tid + "'"
			}
			if aid != "" {
				sql = sql + " and alarm_id ='" + aid + "'"
			}
			if sid != "" {
				sql = sql + " and station_id='" + sid + "'"
			}
			sql = sql + " and  is_hide <> true"

			var result []map[string]interface{}
			err := dbconn.DBConnection.Raw(sql).Find(&result).Error
			if err == nil {
				webapi.SendResult(result, nil, c)
			} else {
				webapi.SendError(err.Error(), webapi.RedisError, c)
			}

		})
		alarmAPI.GET("/cache", func(c *gin.Context) {
			tid := c.Query("tid")
			if tid != "" {
				result := ca.GetThingAlarms(tid)
				webapi.SendResult(result, nil, c)
			} else {
				webapi.SendError("tid为空", webapi.ParamsError, c)
			}

		})
		alarmAPI.GET("/histroy", func(c *gin.Context) {
			tid := c.Query("tid")
			aid := c.Query("aid")
			sindex := c.Query("index")
			sBegin := c.Query("begintime")
			sEnd := c.Query("endtime")
			sql := "select * from alarm_histroys where 1=1 and is_hide=false"
			if sindex != "-1" {
				_, err := strconv.ParseInt(sindex, 10, 64)
				if err == nil {
					sql = sql + " and mindex=" + sindex
				}

			}
			if tid != "" {
				sql = sql + " and t_id='" + tid + "'"
			}
			if aid != "" {
				sql = sql + " and alarm_id='" + aid + "'"
			}

			if sBegin != "" {
				sql = sql + " and alarm_time >='" + sBegin + "'"
			}
			if sEnd != "" {
				sql = sql + " and alarm_time <='" + sEnd + "'"
			}
			if sql != "" {
				sql = sql + " order by alarm_time DESC limit 30000"
			}
			var result []map[string]interface{}
			err := dbconn.DBConnection.Raw(sql).Find(&result).Error
			if err == nil {
				webapi.SendResult(result, nil, c)
			} else {
				webapi.SendError(err.Error(), webapi.RedisError, c)
			}

		})

	}
	initAlarmCfgAPI(alarmAPI)
}
